//
//  MySettingViewController.m
//  DreamSoftReimburse
//
//  Created by 刘应平 on 17/4/26.
//  Copyright © 2017年 ZJ. All rights reserved.
//

#import "MySettingViewController.h"
#import "MySettingTableViewCell.h"
#import "MysettingModel.h"
/**  隐私设置  */
#import "MySettingPrivacyViewController.h"
#import "LMySettingGeneralViewController.h"
#import "JCAlertView.h"
#import "LCustomAlertView.h"
#import "ZJAppUpdateAlertView.h"
/**  问题反馈  */
#import "LPFeedbackQuestionViewController.h"
#import "LoginViewController.h"
#import "JPUSHService.h"
@interface MySettingViewController ()<UITableViewDelegate,UITableViewDataSource,ZJAppUpdateAlertViewDelegate>
{
    NSDictionary *_appUpdateInfo;
    JCAlertView * alertView;
}
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *data;
/**  退出登录  */
@property (strong, nonatomic) UIButton  *backLoginButton;

/**  判断是点的某个cell  */
@property (assign, nonatomic) NSInteger  cell;

@end

@implementation MySettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"设置";
    [self getDataSource];
    [self layoutUI];
}


#pragma mark - 数据
- (void)getDataSource{
    NSString *bane = [NSString stringWithFormat:@"当前版本：%@",CURRENTBANBEN];
    self.data = [NSMutableArray array];
    NSArray *array = @[
  @{@"image": @"mine_set_yinsi", @"title": @"隐私", @"detail": @""},
  @{@"image": @"mine_set_tongyong", @"title": @"通用", @"detail": @""},
  @{@"image": @"mine_set_wentifankui", @"title": @"问题反馈", @"detail": @""},
  @{@"image": @"mine_set_tuisongtongzhi", @"title": @"推送通知", @"detail": @""},
  @{@"image": @"mine_set_banbengengxin", @"title": @"版本更新", @"detail": bane}];
    for (NSDictionary *dic in array) {
        [self.data addObject:[MysettingModel yy_modelWithDictionary:dic]];
    }
   
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.backLoginButton];
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 12 * [UIScreen zj_screenHScale])];
    head.backgroundColor = [UIColor themeGrayColor];
    self.tableView.tableHeaderView = head;
}


#pragma mark - 私有方法
- (void)action_leftButton:(UIButton *)sender{
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 监听事件
/**  退出登录  */
- (void)backButton{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"是否退出登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        /**  这是退出登录  */
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"isLogin"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:USER_INFO];
        [ZJUserInfoModel updateUserInfoFormNSUserDefaults];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
        UIWindow * window =  [[UIApplication sharedApplication].delegate window];
        window.rootViewController = nav;

       
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
           }]];
    [self presentViewController:alertController animated:YES completion:nil];
    

}


#pragma mark - 极光推送
- (void)jpush{
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
           
                [JPUSHService setTags:nil alias:@"" callbackSelector:nil object:nil];
            
        }else{
            NSLog(@"registrationID获取失败，code：%d",resCode);
        }
    }];
    
}

#pragma mark - 协议代理


/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellid = @"cellId";
    MySettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[MySettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.model = self.data[indexPath.row];
    
    return cell;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
        {
            /**  隐私  */
            MySettingPrivacyViewController *vc = [[MySettingPrivacyViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }

            break;
        case 1:
            /**  通用  */
        {
           LMySettingGeneralViewController  *vc = [[LMySettingGeneralViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
            /**  问题反馈  */
        {
            LPFeedbackQuestionViewController *vc = [[LPFeedbackQuestionViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;

        case 3:
            /**  推送通知  */
            break;
        case 4:
            /**  版本更新  */
            [self netWorking_appUpdate];
            break;
        default:
            break;
    }
}


#pragma mark - 软件更新
- (void)netWorking_appUpdate{
    [YJProgressHUD show:@"版本检测中..." inView:self.view mode:YJProgressModeLoading];
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager appUpdate_Success:^(id object) {
        [YJProgressHUD hide];
        if ([object objectForKey:@"version"]) {
            [weakSelf appUpdate_Dict:[object objectForKey:@"version"]];
        }else{
            [YJProgressHUD showMessage:@"检测失败" inView:weakSelf.view];
        }
    } Fail:^(id object) {
         [YJProgressHUD hide];
       [YJProgressHUD showMessage:@"检测失败" inView:weakSelf.view];
    }];
}
///版本对比
- (void)appUpdate_Dict:(NSDictionary *)dict{
    _appUpdateInfo = dict;
    //获取应用程序版本号
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
    //网上版本号
    NSString *newVersionCode = [dict objectForKey:@"versionCode"];
    if ([newVersionCode floatValue] > [currentVersion floatValue]) {//更新
        ZJAppUpdateAlertView *view = [[ZJAppUpdateAlertView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        view.dict = dict;
        view.delegate = self;
        alertView = [[JCAlertView alloc] initWithCustomView:view dismissWhenTouchedBackground:YES];
        [alertView show];
    }else{
    [YJProgressHUD showMessage:@"当前为最新版本" inView:self.view];
    }
}


- (void)ZJAppUpdateAlertViewDelegate_canleClick{
    [alertView dismissWithCompletion:nil];
}

- (void)ZJAppUpdateAlertViewDelegate_updateClick{
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[_appUpdateInfo objectForKey:@"path"]]];
    
}


#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}


- (UIButton *)backLoginButton{
    if (_backLoginButton == nil) {
        _backLoginButton = [[UIButton alloc] initWithFrame:CGRectMake(20 * [UIScreen zj_screenWScale], [UIScreen zj_screenHeight] - 115 * [UIScreen zj_screenHScale], [UIScreen zj_screenWith] - 40 * [UIScreen zj_screenWScale], 50 * [UIScreen zj_screenHScale])];
        _backLoginButton.backgroundColor = [UIColor themeColor];
        _backLoginButton.layer.cornerRadius = 3;
        _backLoginButton.layer.masksToBounds = YES;
        _backLoginButton.titleLabel.font = [UIFont systemFontOfSize:16 * [UIScreen zj_textFontScale]];
        [_backLoginButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [_backLoginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_backLoginButton addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _backLoginButton;

}



@end
